package cn.tedu.rtc.mqtt.model;

import lombok.Getter;

import java.util.Arrays;
import java.util.regex.Pattern;

/**
 * InboundMessageRouter筛选路由枚举类
 */
@Getter
public enum DeviceTopicEnum {

    EVENTS(Pattern.compile("^" + TopicConst.THING_MODEL_PRE + TopicConst.PRODUCT + TopicConst.REGEX_SN + TopicConst.EVENTS_SUF + "$"), ChannelName.INBOUND_EVENTS),

    UNKNOWN(Pattern.compile("^.*$"), ChannelName.DEFAULT);

    Pattern pattern;

    String beanName;

    DeviceTopicEnum(Pattern pattern, String beanName) {
        this.pattern = pattern;
        this.beanName = beanName;
    }

    public static DeviceTopicEnum find(String topic) {
        return Arrays.stream(DeviceTopicEnum.values()).filter(topicEnum -> topicEnum.pattern.matcher(topic).matches()).findAny().orElse(UNKNOWN);
    }
}
